package com.ruyi.afterdemo.action;


import com.example.comm.comm.BaseAction;
import com.example.comm.comm.PageVO;
import com.example.comm.util.DateUtils;
import com.ruyi.afterdemo.po.MDiary;
import com.ruyi.afterdemo.po.OContact;
import com.ruyi.afterdemo.service.MDiaryService;
import com.ruyi.afterdemo.service.StuService;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 时间流逝action
 */
@RestController
@RequestMapping("/MDiary")
public class MDiaryAction extends BaseAction {

    @Resource
    private MDiaryService mDiaryService;

    @PostMapping("/searchByPage")
    public Map searchByPage(@RequestBody Map gson) {
        Map resultMap = new HashMap();
        PageVO<MDiary> pv = new PageVO<>();
        Page<MDiary> p = mDiaryService.findPage(gson);
        pv = pageToVO(p);
        List<MDiary> mDiaries = pv.getContentList();
        mDiaries.stream().forEach(mDiary -> {
            if (mDiary.getStartTime() != null) {
                mDiary.set("show_startTime", DateUtils.TimestampToString_1(mDiary.getStartTime()));
            }
            if (mDiary.getEndTime() != null) {
                mDiary.set("show_endTime", DateUtils.TimestampToString_1(mDiary.getEndTime()));
            }
            if (mDiary.getCreateTime() != null) {
                mDiary.set("show_createTime", DateUtils.TimestampToString_1(mDiary.getCreateTime()));
            }
            if (mDiary.getEndTime() != null && mDiary.getCreateTime() != null) {
                //结束时间距新增时间差(分钟)
                long min = (mDiary.getCreateTime().getTime() - mDiary.getEndTime().getTime())/60000;
                mDiary.set("show_writeTime", String.valueOf(min));
            }
        });
        resultMap.put("total", pv.getTotals());
        resultMap.put("rows", mDiaries);
        resultMap.put("page", pv.getCurrPage());
        return resultMap;
    }


    /**
     * 新增
     *
     * @param mDiary
     * @return
     */
    @PostMapping("/add")
    public Map add(@RequestBody MDiary mDiary) {
        mDiary.setCreateTime(new Timestamp(new Date().getTime()));
        Integer flag = mDiaryService.save(mDiary);
        System.out.println(flag);
        return null;
    }

}
